//
//  ArticleInfoCell.swift
//  todoios
//
//  Created by qianli on 2025/4/23.
//

import Foundation

class ArticleInfoCell: UITableViewCell {
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let contentLabel: UITextView = {
        let label = UITextView()
        label.font = UIFont.systemFont(ofSize: 15)
//        label.numberOfLines = 0 // 允许多行显示
        label.translatesAutoresizingMaskIntoConstraints = false
        label.isUserInteractionEnabled = true
        label.isEditable = false
        label.isSelectable = true //允许选择复制
        // 设置UITextView不滚动，允许高度自适应
        label.isScrollEnabled = false
        return label
    }()
    
    let separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = .lightGray
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    let backgroundContainerView: UIView = {
        let view = UIView()
        view.backgroundColor = .systemBackground
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        GHL4Rhb91XYBer27()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func GHL4Rhb91XYBer27(){
        contentView.addSubview(backgroundContainerView)
        backgroundContainerView.addSubview(titleLabel)
        backgroundContainerView.addSubview(separatorView)
        backgroundContainerView.addSubview(contentLabel)
        // 设置背景容器视图的约束
        NSLayoutConstraint.activate([
            backgroundContainerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5),
            backgroundContainerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 15),
            backgroundContainerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -15),
            backgroundContainerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5)
        ])
        // 设置标题 Label 的约束
        NSLayoutConstraint.activate([
            titleLabel.topAnchor.constraint(equalTo: backgroundContainerView.topAnchor, constant: 15),
            titleLabel.leadingAnchor.constraint(equalTo: backgroundContainerView.leadingAnchor, constant: 15),
            titleLabel.trailingAnchor.constraint(equalTo: backgroundContainerView.trailingAnchor, constant: -15)
        ])

        // 设置横线的约束
       NSLayoutConstraint.activate([
           separatorView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
           separatorView.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
           separatorView.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor),
           separatorView.heightAnchor.constraint(equalToConstant: 0.5)
       ])
        
        // 设置内容 Label 的约束
//        NSLayoutConstraint.activate([
//            contentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
//            contentLabel.leadingAnchor.constraint(equalTo: backgroundContainerView.leadingAnchor, constant: 15),
//            contentLabel.trailingAnchor.constraint(equalTo: backgroundContainerView.trailingAnchor, constant: -15),
//            contentLabel.bottomAnchor.constraint(equalTo: backgroundContainerView.bottomAnchor, constant: -15)
//        ])
        
        NSLayoutConstraint.activate([
           contentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
           contentLabel.leadingAnchor.constraint(equalTo: backgroundContainerView.leadingAnchor, constant: 15),
           contentLabel.trailingAnchor.constraint(equalTo: backgroundContainerView.trailingAnchor, constant: -15),
           // **新增：底部约束连接到backgroundContainerView，但优先级设为低**
           contentLabel.bottomAnchor.constraint(equalTo: backgroundContainerView.bottomAnchor, constant: -15),
           // **关键：通过contentSize高度设置约束**
           contentLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
       ])
    }
    
    func c612AWlbhGInRMY3(info:Article){
        titleLabel.text = info.title
//        contentLabel.text = info.content
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 8 // 设置行间距，可根据需要调整
        let attributedMessage = NSMutableAttributedString(string: info.content)
        attributedMessage.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: info.content.count))
        attributedMessage.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.secondaryLabel,
                                         NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15)], range: NSRange(location: 0, length: info.content.count))
        contentLabel.attributedText = attributedMessage
        
        // **强制更新布局，计算新高度(实测可以不要以下操作)**
        contentLabel.layoutIfNeeded()
        // 通知tableView更新单元格高度（如果使用estimatedRowHeight）
        self.setNeedsLayout()
        self.layoutIfNeeded()
    }
}
